# coding=utf-8
"""
author: jamon
"""
from typing import Type

from obespoir.share.singleton import Singleton
from obespoir.websocketserver.protocol import WebSocketProtocol
from obespoir.websocketserver.route import WebsocketController

from obespoir.share.espoir_log import global_log as logger
from package.msg_const import MsgID


class WebSocketClientController(WebsocketController, metaclass=Singleton):

    def __init__(self):
        super().__init__()

    async def after_open_handler(self, ws: Type[WebSocketProtocol]):
        """连接建立后执行的特殊逻辑, 如可以做客户端热更"""
        logger.info("start")

        from server.client_hotfix import file_list
        if file_list.hotfix_code:
            logger.info("[send_hotfix_code][file=%s]",
                        file_list.hotfix_code.keys())
            for code in file_list.hotfix_code.itervalues():
                await ws.send_message(result=code, command_id=MsgID.HOTFIX)

    async def after_close_handler(self, ws: Type[WebSocketProtocol]):
        """连接断开时执行的特殊逻辑，如部分缓存的清理"""
        logger.info("start")
